﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Core.Extensions;
using App.Data;

public partial class Pages_Admin_ContentChange : BasePage
{
    private int _ID = 0;
    private bool _IsEditMode = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            BindDropDownLists();
            BindContentInfo();
            ShowSuccessMessage();
        }
    }
    /// <summary>
    /// Bindis the Page Initialization Variables
    /// </summary>
    protected void BindPageInfo()
    {
        _ID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.ID);
        if (_ID > 0)
        {
            _IsEditMode = true;
            h4Heading.InnerHtml = "Edit Content";
        }
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }

    /// <summary>
    /// Binds Dropdownlists for the initial request.
    /// </summary>
    protected void BindDropDownLists()
    {
        //BindDropdownList.Users(ddlModifiedBy);
    }
    protected void ShowSuccessMessage()
    {
        if(String.Compare(WebUtil.GetQueryStringInString(AppConstants.QueryString.SUCCESS_MESSAGE), "True", false) == 0)
        {
            WebUtil.ShowMessageBox(divMessage, "Content Saved Successfully.", false);
        }
    }
    /// <summary>
    /// Binds Content Info Requested through Query Strings
    /// </summary>
    protected void BindContentInfo()
    {
        if (_IsEditMode)
        {
            BtcHousingEntities context = new BtcHousingEntities();
            App.Data.Content entity = context.Contents.FirstOrDefault(P => P.ID == _ID);//dao.GetByID(_ID);
            if (entity == null)
                ShowErrorMessage();
            else
            {
                txtTitle.Text = entity.Title;
                txtDescription.Text = entity.Description;
                txtSummery.Text = entity.Summery;
            }
        }
    }
    /// <summary>
    /// Shows a Message in the UI and Hides the Data Editing Controls
    /// </summary>
    protected void ShowErrorMessage()
    {
        pnlFormContainer.Visible = false;
        WebUtil.ShowMessageBox(divMessage, "Requested Content was not found.", true);
    }
    protected void SaveContent()
    {
        BtcHousingEntities context = new BtcHousingEntities();
        App.Data.Content entity = null;

        if (_IsEditMode)
            entity = context.Contents.FirstOrDefault(P => P.ID == _ID); //dao.GetByID(_ID);        
        else
        {
            entity = new App.Data.Content();
            entity.Created = DateTime.Now;
            context.AddToContents(entity);
        }

        entity.Title = txtTitle.Text;
        entity.Description = txtDescription.Text;
        entity.Summery = txtSummery.Text;
        entity.ModifiedBy = SessionCache.CurrentUser.ID;
        entity.Modified = DateTime.Now;
        context.SaveChanges();

        String url = String.Format("{0}?{1}=True", Request.Url.AbsolutePath, AppConstants.QueryString.SUCCESS_MESSAGE);
        Response.Redirect(url);
        //WebUtil.ShowMessageBox(divMessage, "Content Saved Successfully.", false);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SaveContent();
            //Response.Redirect(AppConstants.Pages.CONTENT_LIST);
            return;
        }
    }
    protected void btnList_Click(object sender, EventArgs e)
    {
        String url = String.Format("{0}?{1}={2}&{3}=True", Request.Url.AbsolutePath, AppConstants.QueryString.ID, AppConstants.QueryString.SUCCESS_MESSAGE);
        Response.Redirect(url);
        return;
    }
}